package tuna;

import java.sql.*;

public class DBConnection {
	protected Connection con;
    protected boolean isConnected;
    
    private Statement statement;
    private String dbName;
	private String password;
	private String host;
	private String user;
    
	/**
	 * Constructor
	 */
    public DBConnection (String host, String db, String user, String password) {
    	this.host = host;
		this.user = user;
		this.password = password;
		this.dbName = db;
    }
    
    /**
     * Connect to the server DB
     */
    public void connectDB() {   	
    	try {	
			Driver driver = (Driver) Class.forName("com.mysql.jdbc.Driver").newInstance();
			DriverManager.registerDriver(driver);
			
			
			String url = "jdbc:mysql://" + host + "/" + dbName;
				
			con = DriverManager.getConnection(url,user, password);
			statement = con.createStatement();
			
			isConnected = true;
		}
		catch(Exception ex) {
			ex.printStackTrace();
			
			isConnected = false;
		}
    }
       
    /**
     * Disconnect from the server DB 
     */
	public void disconnectDB(){
		try {
			if(statement!=null) {
				statement.close();
			}
			if(con!=null) {
				con.close();
			}
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}

		host = "";
		user = "";
		password = "";
		dbName = "";
		
		isConnected = false;
	}
	
	
	/**
     * Used to execute update type of query, which only modifies the data in a table.
     * (ie. insert, update)
     * @param query Query to execute for the statement.
     * @return true if the operation is successful
     */    
    public boolean updateQuery(String query) {
    	try {
    		int success = statement.executeUpdate(query);
    		
    		if(success <= 0) {
    			return false;
    		}
    		else {
    			return true;
    		}
    	}
        catch (SQLException sqlException) {
            sqlException.printStackTrace();
            System.out.println("Failed to update DB: " + query);
            System.out.println("discon: update");
			try {
				disconnectDB();
			} 
			catch (Exception e) {
				e.printStackTrace();
			}
			return false;
        }
        catch (Exception exception) {
            exception.printStackTrace();
            System.out.println("discon: update");
            try {
				disconnectDB();
			} 
            catch (Exception e) {
				e.printStackTrace();
			}
            return false;
        }
    }
    
    
    /**
     * Used to execute run type of query, which returns ResultSet defined by query.
     * (ie. select query)
     * @param query Query to execute for the statement.
     * @return resultset that contains data defined by query.
     */    
    public ResultSet runQuery(String query) {
    	ResultSet result = null;
    	try {
	        result = statement.executeQuery(query);
    	}
        catch (SQLException sqlException) {
            sqlException.printStackTrace();
            System.out.println("Failed to execute: " + query);
            System.out.println("discon: run");
            try {
				disconnectDB();
			} 
            catch (Exception e) {
				e.printStackTrace();
			}
        }
        catch (Exception exception) {
            exception.printStackTrace();
            System.out.println("discon: run");
            try {
            	disconnectDB();
			} 
            catch (Exception e) {
				e.printStackTrace();
			}
        }
        return result;
    }
}
